/*
 * RoboEye - Web client for www.newocr.com that allows you
 * to recognize an image captured by the camera.
 *
 * Copyright (C) Oxygen Development, 2011
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

package com.devoxy.roboeye.ui;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
import com.devoxy.roboeye.R;
import com.devoxy.roboeye.core.FileHelper;
import com.devoxy.roboeye.core.provider.Task;
import com.devoxy.roboeye.global.ContextHolder;
import com.devoxy.roboeye.global.IntentHelper;
import com.devoxy.roboeye.global.PreferencesHelper;

/**
 * Created by Oxygen Development.
 * Author: Dmitriy Tarasov (dm.vl.tarasov@gmail.com)
 * Date: 04.05.11
 * Time: 1:57
 */
public class NotificationHelper {

    private static final int TASK_FINISHED_NOTIFICATION = 1;

    public static void showText(Context context, int stringId) {
        Toast.makeText(context, stringId, Toast.LENGTH_LONG).show();
    }

    public static void showText(Context context, String text) {
        Toast.makeText(context, text, Toast.LENGTH_LONG).show();
    }

    public static void taskFinished(Task task) {
        // TODO notification icon with cool number in top-left corner
        Context context = ContextHolder.getInstance().getContext();
        NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

        int icon = R.drawable.icon;
        CharSequence tickerText = context.getString(R.string.task_finished);
        long when = System.currentTimeMillis();

        Notification notification = new Notification(icon, tickerText, when);
        notification.flags |= Notification.FLAG_AUTO_CANCEL;
        notification.defaults |= Notification.DEFAULT_LIGHTS;
        Boolean vibrationEnabled = PreferencesHelper.getPref(R.string.vibration_pref);
        if (vibrationEnabled) {
            notification.defaults |= Notification.DEFAULT_VIBRATE;
        }

        CharSequence contentTitle = FileHelper.getFileNameFromPath(task.getSource());
        CharSequence contentText = FileHelper.getFileNameFromPath(task.getTarget());
        Intent notificationIntent = IntentHelper.getTaskListIntent();
        PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
        notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
        notificationManager.notify(TASK_FINISHED_NOTIFICATION, notification);
    }
}
